/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.azon.ip.utils;

import com.chain.code.utils.MatrixConverter;
import com.azon.guesser.utils.FilledChecker;
import com.azon.guesser.utils.ThinnedChecker;
import com.chain.code.utils.WetLeftHand;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Mahdan Ahmad F A
 */
public class Printer {
    public static void printMatrix(int[][] matrix) {
        String current = "";
        
        for (int m = 0; m < matrix.length; m++) {
            for (int n = 0; n < matrix[0].length; n++) {
                current = "" + matrix[m][n];
                
                if (current.length() == 1) {
                    System.out.print("00" + current + " ");
                } else if (current.length() == 2) {
                    System.out.print("0" + current + " ");
                } else {
                    System.out.print(current + " ");
                }
            }
            System.out.println("");
        }
    }
    
    public static void printBinaryMatrix(int[][] matrix) {
        for (int m = 0; m < matrix.length; m++) {
            for (int n = 0; n < matrix[0].length; n++) {
                System.out.print(matrix[m][n] + " ");
            }
            System.out.println("");
        }        
    }
    
    public static void printMap(HashMap map) {
        System.out.println("");
        System.out.println("**************************************************************************");
        System.out.println("Chain code        : " + map.get("Chain code"));
        System.out.println("Sequence          : " + map.get("Sequence"));
        System.out.println("Enhanced Sequence : " + map.get("Enhanced"));
        System.out.println("Position          : " + map.get("Position"));
        System.out.println("Titik             : " + map.get("Titik"));
        System.out.println("Garis Horizontal  : " + map.get("Garis Horizontal"));
        System.out.println("Garis Vertical    : " + map.get("Garis Vertical"));
        System.out.println("Garis Miring      : " + map.get("Garis Miring"));
        System.out.println("Cembung Kanan     : " + map.get("Cembung Kanan"));
        System.out.println("Cembung Kiri      : " + map.get("Cembung Kiri"));
        System.out.println("Cembung Atas      : " + map.get("Cembung Atas"));
        System.out.println("Cembung Bawah     : " + map.get("Cembung Bawah"));
        System.out.println("**************************************************************************");
    }
    
    public static void printToCSV(String path, PrintWriter writer, WetLeftHand wet, int[][] matrix_grayscale, int[][] matrix_converted, boolean flagThinned, HashMap<String, String> map0, HashMap<String, String> map1, HashMap<String, String> map2, HashMap<String, String> map3) {
        
        String karakter     = path.substring(path.indexOf("\\")+1, path.indexOf("."));
        
        System.out.println(karakter);
        
        matrix_grayscale    = ImageReader.ReadImageAsGrayScale(path);
        matrix_converted    = MatrixConverter.blackAndWhiteConverter(matrix_grayscale);
        
        String[] result = wet.doWetLeftHand(matrix_converted);

        if (flagThinned) {
            map0 = ThinnedChecker.doCheck(result[0]);
            map1 = ThinnedChecker.doCheck(result[1]);
            map2 = ThinnedChecker.doCheck(result[2]);
            map3 = ThinnedChecker.doCheck(result[3]);
        } else  {
            map0 = FilledChecker.doCheck(result[0]);
            map1 = FilledChecker.doCheck(result[1]);
            map2 = FilledChecker.doCheck(result[2]);
            map3 = FilledChecker.doCheck(result[3]); 
        }
        String[] index  = getFontType(map0, map1, map2, map3);
        
        writer.println(getElement(map0, karakter, index[0]));
        writer.println(getElement(map1, karakter, index[1]));
        writer.println(getElement(map2, karakter, index[2]));
        writer.println(getElement(map3, karakter, index[3]));
    }
    
    private static String getElement(HashMap<String, String> map, String karakter, String index) {
        String position  = map.get("Position");
        //System.out.println(position);
        
        String positionY = position.substring(position.indexOf(",") + 1, position.indexOf(")"));
        
        return (map.get("Titik") + ", " + map.get("Garis Horizontal") + ", " + map.get("Garis Vertical") + ", " + map.get("Garis Miring")+ ", " + map.get("Cembung Kanan")+ ", " + map.get("Cembung Kiri")+ ", " + map.get("Cembung Atas")+ ", " + map.get("Cembung Bawah")+ ", " + (int)karakter.charAt(0) + ", " + karakter.charAt(0) + ", " + index);
    }
    
    private static String[] getFontType(HashMap<String, String> map0, HashMap<String, String> map1, HashMap<String, String> map2, HashMap<String, String> map3) {
        int Y0 = getValueY(map0);
        int Y1 = getValueY(map1);
        int Y2 = getValueY(map2);
        int Y3 = getValueY(map3);
        
        String[] index    = new String[4];
        int[] sortedValue = getSortedValue(Y0, Y1, Y2, Y3);
        
        index[0] = "" + getSequenceIndex(sortedValue, Y0);
        index[1] = "" + getSequenceIndex(sortedValue, Y1);
        index[2] = "" + getSequenceIndex(sortedValue, Y2);
        index[3] = "" + getSequenceIndex(sortedValue, Y3);
        
        /*
        System.out.println(Y0 + " " + Y1 + " " + Y2 + " " + Y3);
        
        for (int i = 0; i < sortedValue.length; i++) {
            System.out.print(sortedValue[i] + " ");
        }
        
        System.out.println("");
        
        for (int i = 0; i < index.length; i++) {
            System.out.print(index[i] + " ");
        }
        
        System.out.println("");
        // */
        return index;
    }
    
    private static int getValueY(HashMap<String, String> map) {
        String position = map.get("Position");
        String valueY   = position.substring(position.indexOf(",") + 1, position.indexOf(")"));
        
        return Integer.parseInt(valueY);
    }    
   
    private static int[] getSortedValue(int Y0, int Y1, int Y2, int Y3) {
        
        ArrayList<Integer> cache = new ArrayList<>();
        int[] array = new int[4];
        
        cache.add(Y0);
        cache.add(Y1);
        cache.add(Y2);
        cache.add(Y3);
        
        int minValue = 0;
        int iterator = 0;
        
        while(!(cache.isEmpty())) {
            minValue        = getMinValue(cache);
            array[iterator] = minValue;
            
            cache.remove(cache.indexOf(minValue));
            iterator++;
        } // */
        
        /*
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        // */
        //System.out.println(minValue + " " + cache.indexOf(minValue));
        
        return array;
    }
    
    private static int getMinValue(ArrayList<Integer> array) {
        int temp = array.get(0);
        
        for (int i = 1; i < array.size(); i++) {
            if (array.get(i) < temp) {
                temp = array.get(i);
            }
        }
        
        return temp;
    }
    
    private static int getSequenceIndex(int[] array, int value) {
        
        for (int i = 0; i < array.length; i++) {
            if (array[i] == value) {
                return i + 1;
            }
        }
        
        return 28;
    }
}
